package ControlLayer;
import ModelLayer.*;
import java.util.ArrayList;
import java.util.Iterator;


/**
 * Controller class for the address book
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressCtr
{
    private AddressBook addressBook;
    
    /* Constructor for class AddressCtr */
    public AddressCtr()
    {
        addressBook = new AddressBook();
    }
    
    /* Function to search the container and print information for a sepcific person */
    public void getPerson(long id)
    {
         boolean find = true;
            for(Person object : addressBook.friends) {
                if(object.getId() == id) 
                    {
                    find = false;
                    System.out.println("ID : " + object.getId());
                    System.out.println("Name : " + object.getName());
                    System.out.println("Address : " + object.getAddress());
                    System.out.println("Postal code : " + object.getPostalCode());
                    System.out.println("City : " + object.getCity());
                    System.out.println("Phone : " + object.getPhone());
                    }   
            }
            
            if(find) {
                System.out.println("No match");
            }   
    }
    
    /* Method to creates a Person object and stores it in the container */ 
    public void createPerson(long id, String name, String address, String postalCode, String city, String phone)
    {  
        Person people = new Person(id, name, address, postalCode, city, phone);
            if(addressBook.friends.contains(people)) {
                System.out.println("Person already in the addressbook");
            }
            else { 
                addressBook.friends.add(people);
            }
    }
    
    /* Method to search for the person and delete the object from the container */
    public void deletePerson(long id)
    {
        Iterator<Person> it = addressBook.friends.iterator();
            while(it.hasNext()) {   
                Person p = it.next();
                if(p.getId() == id) {
                    it.remove();
                }
                else {
                    System.out.println("ID invalid");
                }   
            }        
    }
    
    /* Method to search and update a person */
    public void updatePerson(long id, String name, String address, String postalCode, String city, String phone )
    {
         Person person = findPerson(id);
            if (person != null) {
                person.setName(name);
                person.setAddress(address);
                person.setPostalCode(postalCode);
                person.setCity(city);
                person.setPhone(phone);  
            }
    }
    
    /* Method to print all persons */
    public void listAllPersons()
    {    
        for(Person person : addressBook.friends) {  
           person.printDetails();
        }   
    }
    
    
    /* Method to find a specific person */
    public Person findPerson(long id)
    {
        for (Person person : addressBook.friends) {
            if (person.getId() == id) {
                return person;
            }
        }
        return null;
    }
    
}
